package com.jrx.anytxn.param.service.partner.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.partner.PrVerifyFileTableReq;
import com.jrx.anytxn.param.dto.partner.PrVerifyFileTableRes;
import com.jrx.anytxn.param.entity.PrVerifyFileTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrVerifyFileTableMapper;
import com.jrx.anytxn.param.service.partner.IPrVerifyFileTableService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 对账文件参数service实现
 */
@CacheConfig(cacheNames = "VERIFY_FILE")
@Service
public class PrVerifyFileTableServiceImpl implements IPrVerifyFileTableService {

    private static final Logger logger = LoggerFactory.getLogger(PartnerShareProfitServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtPrVerifyFileTableMapper extPrVerifyFileTableMapper;

    @Override
    public PrVerifyFileTableRes getByTenIdIdAndVerifyFileId(String tenantId, String verifyFileId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(verifyFileId)) {
            logger.error("商户ID或参数ID为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrVerifyFileTable prShareProfitTable = extPrVerifyFileTableMapper.selectByVerifyFileIdAndTenantId(verifyFileId,tenantId);
        if (null == prShareProfitTable) {
            logger.error("未查到对账文件配置参数信息");
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(prShareProfitTable, PrVerifyFileTableRes.class);
    }

    @Override
    public PrVerifyFileTableRes addPrVerifyFileTable(PrVerifyFileTableReq prVerifyFileTableReq) throws TxnException {
        PrVerifyFileTable table = extPrVerifyFileTableMapper.selectByVerifyFileIdAndTenantId(
                prVerifyFileTableReq.getVerifyFileId(),prVerifyFileTableReq.getTenantId());
        if (table != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrVerifyFileTable prShareProfitTable = BeanMapping.copy(prVerifyFileTableReq, PrVerifyFileTable.class);
        try {
            prShareProfitTable.setId(segmentService.getId(Constant.PR_VERIFY_FILE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prShareProfitTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrVerifyFileTableMapper.insertSelective(prShareProfitTable);
        return BeanMapping.copy(prVerifyFileTableReq, PrVerifyFileTableRes.class);
    }

    @Override
    public List<PrVerifyFileTableRes> findAllByTenId(String tenantId) {
        List<PrVerifyFileTable> prVerifyFileTables = extPrVerifyFileTableMapper.selectByTenIdAndStatus(tenantId);
        return BeanMapping.copyList(prVerifyFileTables, PrVerifyFileTableRes.class);
    }

    @Override
    public TxnPage<PrVerifyFileTableRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrVerifyFileTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrVerifyFileTable> tableList = extPrVerifyFileTableMapper.selectByTenId(tenantId);
        if (CollectionUtils.isEmpty(tableList)) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrVerifyFileTableRes> res = BeanMapping.copyList(tableList, PrVerifyFileTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @CacheEvict(key="'verify_'+#prVerifyFileTableReq.tenantId+'_'+#prVerifyFileTableReq.verifyFileId")
    @Override
    public PrVerifyFileTableRes modifyPrVerifyFileTable(PrVerifyFileTableReq prVerifyFileTableReq) throws TxnBizException {
        PrVerifyFileTable table = extPrVerifyFileTableMapper.selectByVerifyFileIdAndTenantId(prVerifyFileTableReq.getVerifyFileId(),
                prVerifyFileTableReq.getTenantId());
        if (table == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PrVerifyFileTable prShareProfitTable = BeanMapping.copy(prVerifyFileTableReq, PrVerifyFileTable.class);
        // 更新
        extPrVerifyFileTableMapper.updateByVerifyFileIdAndTenantId(prShareProfitTable);
        return BeanMapping.copy(prVerifyFileTableReq, PrVerifyFileTableRes.class);
    }

    @CacheEvict(key="'verify_'+#tenantId+'_'+#verifyFileId")
    @Override
    public Boolean removeByTenantIdAndVerifyFileId(String tenantId, String verifyFileId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(verifyFileId)) {
            logger.error("租户id或参数id不可为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrVerifyFileTableMapper.deleteByVerifyFileIdAndTenantId(verifyFileId,tenantId);
        return i > 0;
    }

    @Cacheable(key="'verify_'+#tenantId+'_'+#verifyFileId")
    @Override
    public PrVerifyFileTable getByVerifyFileId(String tenantId, String verifyFileId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(verifyFileId)) {
            logger.error("租户ID或参数id为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrVerifyFileTable table = extPrVerifyFileTableMapper.selectByVerifyFileIdAndTenantId(
                verifyFileId,tenantId);
        return table;
    }

    @Override
    public List<PrVerifyFileTableRes> getVerifyFileByOrgIdAndTenId(String organizationId, String tenantId) {
        List<PrVerifyFileTable> prVerifyFileTables = extPrVerifyFileTableMapper.selectByTenIdAndOrgId(organizationId,tenantId);
        return BeanMapping.copyList(prVerifyFileTables, PrVerifyFileTableRes.class);
    }
}
